package org.rsbot.script.randoms;

import java.util.ArrayList;
import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.NPCs;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;

@ScriptManifest(authors={"PwnZ"}, name="Molly", version=1.9D)
public class Molly extends Random
{
  static final int CLAW_ID = 14976;
  static final int CONTROL_PANEL_ID = 14978;
  static final int DOOR_ID = 14982;
  static final int MOLLY_CHATBOX_INTERFACEGROUP = 228;
  static final int MOLLY_CHATBOX_NOTHANKS = 3;
  static final int MOLLY_CHATBOX_SKIP = 2;
  static final int CONTROL_INTERFACEGROUP = 240;
  static final int CONTROLS_GRAB = 28;
  static final int CONTROLS_UP = 29;
  static final int CONTROLS_DOWN = 30;
  static final int CONTROLS_LEFT = 31;
  static final int CONTROLS_RIGHT = 32;
  static final int CONTROLS_EXIT = 33;
  private RSNPC molly;
  private RSObject controlPanel;
  private int mollyID = -1;
  private boolean cameraSet;
  private boolean talkedToMolly;
  private boolean finished;
  private long delayTime;

  public boolean activateCondition()
  {
    this.molly = this.npcs.getNearest(new String[] { "Molly" });
    this.controlPanel = this.objects.getNearest(new int[] { 14978 });
    return ((this.molly != null) && (this.molly.isInteractingWithLocalPlayer())) || (this.controlPanel != null);
  }

  private boolean inControlInterface() {
    RSInterface localRSInterface = this.interfaces.get(240);
    return (localRSInterface != null) && (localRSInterface.isValid());
  }

  private boolean inControlRoom() {
    RSObject localRSObject = this.objects.getNearest(new int[] { 14982 });
    return (localRSObject != null) && (getMyPlayer().getLocation().getX() > localRSObject.getLocation().getX());
  }

  public int loop()
  {
    if (!activateCondition()) {
      log("Molly random finished!");
      sleep(500);
      if (!activateCondition()) {
        return -1;
      }
    }
    this.controlPanel = this.objects.getNearest(new int[] { 14978 });
    while ((getMyPlayer().isMoving()) || (getMyPlayer().getAnimation() != -1)) {
      sleep(random(800, 1300));
    }
    if (this.interfaces.canContinue()) {
      this.interfaces.clickContinue();
      return random(250, 750);
    }
    if (this.mollyID == -1) {
      this.mollyID = this.molly.getID();
      log("Molly ID: " + Integer.toString(this.mollyID));
      log("Evil Twin ID:" + Integer.toString(this.mollyID - 40));
    }
    if (this.interfaces.canContinue()) {
      setCamera();
      this.interfaces.clickContinue();
      return random(500, 800);
    }
    RSComponent localRSComponent1 = this.interfaces.get(228).getComponent(2);
    if ((localRSComponent1 != null) && (localRSComponent1.isValid()) && (localRSComponent1.getAbsoluteY() > 5) && (localRSComponent1.containsText("Yes, I"))) {
      localRSComponent1.doClick();
      return random(600, 1000);
    }
    RSComponent localRSComponent2 = this.interfaces.get(228).getComponent(3);
    if ((localRSComponent2 != null) && (localRSComponent2.isValid()) && (localRSComponent2.getAbsoluteY() > 5)) {
      setCamera();
      sleep(random(800, 1200));
      localRSComponent2.doClick();
      this.talkedToMolly = true;
      return random(600, 1000);
    }
    if (!this.cameraSet) {
      this.camera.setPitch(true);
      this.cameraSet = true;
      return random(300, 500);
    }
    if ((this.finished) && (!inControlRoom())) {
      if (!this.calc.tileOnScreen(this.molly.getLocation())) {
        this.walking.walkTileOnScreen(this.molly.getLocation());
        return random(1000, 2000);
      }
      this.molly.doAction("Talk");
      return random(1000, 1200);
    }
    if ((this.finished) && (inControlRoom())) {
      if (!openDoor()) {
        return random(400, 500);
      }
      return random(400, 600);
    }
    if (!inControlRoom()) {
      if ((this.talkedToMolly) && (!this.finished) && ((this.interfaces.get(228) == null) || (this.interfaces.get(228).getComponent(0).getAbsoluteY() < 2)) && ((this.interfaces.get(3) == null) || (this.interfaces.get(3).getComponent(0).getAbsoluteY() < 2)))
      {
        openDoor();
        sleep(random(800, 1200));
      } else {
        this.molly.doAction("Talk");
        this.talkedToMolly = true;
        sleep(random(800, 1200));
      }
    }
    else if (this.npcs.getNearest(new String[] { "Molly" }) != null) {
      this.finished = true;
      sleep(random(800, 1200));
    }
    else if (!inControlInterface()) {
      if (this.calc.tileOnScreen(this.controlPanel.getLocation())) {
        this.controlPanel.doAction("Use");
        sleep(random(1200, 2000));
      } else {
        this.walking.walkTileOnScreen(this.controlPanel.getLocation());
        this.camera.setPitch(true);
        this.camera.turnToObject(this.controlPanel);
      }
    } else {
      navigateClaw();
      this.delayTime = System.currentTimeMillis();
      while ((!this.interfaces.canContinue()) && (System.currentTimeMillis() - this.delayTime < 15000L));
      if (this.interfaces.canContinue()) {
        this.interfaces.clickContinue();
      }
      sleep(random(300, 400));
    }

    return random(200, 400);
  }

  private void navigateClaw() {
    if ((!inControlInterface()) || (this.mollyID < 1))
      return;
    RSObject localRSObject;
    while ((localRSObject = this.objects.getNearest(new int[] { 14976 })) != null)
    {
      RSNPC localRSNPC;
      if ((localRSNPC = this.npcs.getNearest(new int[] { this.mollyID - 40 })) == null) break;
      RSTile localRSTile1 = localRSObject.getLocation();
      RSTile localRSTile2 = localRSNPC.getLocation();
      ArrayList localArrayList = new ArrayList();
      if (localRSTile2.getX() > localRSTile1.getX()) {
        localArrayList.add(Integer.valueOf(31));
      }
      if (localRSTile2.getX() < localRSTile1.getX()) {
        localArrayList.add(Integer.valueOf(32));
      }
      if (localRSTile2.getY() > localRSTile1.getY()) {
        localArrayList.add(Integer.valueOf(30));
      }
      if (localRSTile2.getY() < localRSTile1.getY()) {
        localArrayList.add(Integer.valueOf(29));
      }
      if (localArrayList.isEmpty()) {
        localArrayList.add(Integer.valueOf(28));
      }
      RSInterface localRSInterface = this.interfaces.get(240);
      if ((localRSInterface != null) && (localRSInterface.isValid())) {
        localRSInterface.getComponent(((Integer)localArrayList.get(random(0, localArrayList.size()))).intValue()).doClick();
      }
      this.delayTime = System.currentTimeMillis();
      while ((!hasClawMoved(localRSTile1)) && (System.currentTimeMillis() - this.delayTime < 3500L))
        sleep(10);
    }
  }

  private boolean hasClawMoved(RSTile paramRSTile)
  {
    RSObject localRSObject = this.objects.getNearest(new int[] { 14976 });
    if (localRSObject == null)
      return false;
    RSTile localRSTile = localRSObject.getLocation();
    return (localRSTile.getX() - paramRSTile.getX() != 0) || (localRSTile.getY() - paramRSTile.getY() != 0);
  }

  private boolean openDoor() {
    RSObject localRSObject = this.objects.getNearest(new int[] { 14982 });
    if (localRSObject == null)
      return false;
    if (!this.calc.tileOnScreen(localRSObject.getLocation())) {
      this.walking.walkTileOnScreen(localRSObject.getLocation());
      sleep(1000, 2000);
      return false;
    }
    localRSObject.doAction("Open");
    return false;
  }

  private void setCamera() {
    if ((random(0, 6) == 3) && (!this.cameraSet)) {
      this.camera.setPitch(true);
      this.cameraSet = true;
    }
  }
}